package config

import (
	"fmt"
	"github.com/spf13/viper"
	"go.uber.org/zap/zapcore"
	"os"
)

// 全局配置变量
var Conf = new(config)

type config struct {
	System   *SystemConfig   `mapstructure:"system" json:"system"`
	Mysql    *MysqlConfig    `mapstructure:"mysql" json:"mysql"`
	Redis    *RedisConfig    `mapstructure:"redis" json:"redis"`
	Es       *EsConfig       `mapstructure:"es" json:"es"`
	RabbitMq *RabbitMqConfig `mapstructure:"rabbitmq" json:"rabbitMq"`
	Qiniu    *QiniuConfig    `mapstructure:"qiniu" json:"qiniu"`
	Jwt      *JwtConfig      `mapstructure:"jwt" json:"jwt"`
	Logs     *LogsConfig     `mapstructure:"logs" json:"logs"`
}

type MysqlConfig struct {
	Username    string `mapstructure:"username" json:"username"`
	Password    string `mapstructure:"password" json:"password"`
	Database    string `mapstructure:"database" json:"database"`
	Host        string `mapstructure:"host" json:"host"`
	Port        int    `mapstructure:"port" json:"port"`
	Query       string `mapstructure:"query" json:"query"`
	LogMode     bool   `mapstructure:"logger-mode" json:"logMode"`
	TablePrefix string `mapstructure:"table-prefix" json:"tablePrefix"`
	Charset     string `mapstructure:"charset" json:"charset"`
	Collation   string `mapstructure:"collation" json:"collation"`
}

type QiniuConfig struct {
	AK        string `mapstructure:"ak" json:"ak" yaml:"ak"`
	SK        string `mapstructure:"sk" json:"sk" yaml:"sk"`
	Bucket    string `mapstructure:"bucket" json:"bucket" yaml:"bucket"`
	Url       string `mapstructure:"url" json:"url" yaml:"url"`
	UploadUrl string `mapstructure:"upload-url" json:"uploadUrl" yaml:"uploadUrl"`
}

type RedisConfig struct {
	Addr     string `mapstructure:"addr" json:"addr" yaml:"addr"`             // 服务器地址:端口
	Password string `mapstructure:"password" json:"password" yaml:"password"` // 密码
	DB       int    `mapstructure:"db" json:"db" yaml:"db"`                   // redis的哪个数据库
}

type EsConfig struct {
	Username string `mapstructure:"username" json:"username"`
	Password string `mapstructure:"password" json:"password"`
	Host     string `mapstructure:"host" json:"host"`
	Port     int    `mapstructure:"port" json:"port"`
}

type RabbitMqConfig struct {
	Host            string `mapstructure:"host" json:"host" yaml:"host"`
	Port            string `mapstructure:"port" json:"port" yaml:"port"`
	Username        string `mapstructure:"username" json:"username" yaml:"username"`
	Password        string `mapstructure:"password" json:"password" yaml:"password"`
	AcknowledgeMode string `mapstructure:"acknowledge-mode" json:"acknowledgeMode" yaml:"acknowledge-mode"` // 确认方式
}

type SystemConfig struct {
	Mode          string `mapstructure:"mode" json:"mode"`
	UrlPathPrefix string `mapstructure:"url-path-prefix" json:"urlPathPrefix"`
	Port          string `mapstructure:"port" json:"port"`
}

type JwtConfig struct {
	Realm      string `mapstructure:"realm" json:"realm"`
	Key        string `mapstructure:"key" json:"key"`
	Timeout    int    `mapstructure:"timeout" json:"timeout"`
	MaxRefresh int    `mapstructure:"max-refresh" json:"maxRefresh"`
}

type LogsConfig struct {
	Level      zapcore.Level `mapstructure:"level" json:"level"`
	Path       string        `mapstructure:"path" json:"path"`
	MaxSize    int           `mapstructure:"max-size" json:"maxSize"`
	MaxBackups int           `mapstructure:"max-backups" json:"maxBackups"`
	MaxAge     int           `mapstructure:"max-age" json:"maxAge"`
	Compress   bool          `mapstructure:"compress" json:"compress"`
}

// 读取配置文件
func InitConfig(configFile string) {
	workDir, err := os.Getwd()
	if err != nil {
		panic(fmt.Errorf("读取应用目录失败:%s \n", err))
	}
	viper.SetConfigName(configFile)
	viper.SetConfigType("yml")
	viper.AddConfigPath(workDir)
	// 读取配置信息
	err = viper.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("读取配置文件失败:%s \n", err))
	}
	// 将读取的配置信息保存至全局变量Conf
	if err := viper.Unmarshal(Conf); err != nil {
		panic(fmt.Errorf("初始化配置文件失败:%s \n", err))
	}
}
